package com.hsh.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsh.erp.domain.common.Result;
import com.hsh.erp.domain.dto.SupplierDTO;
import com.hsh.erp.domain.entity.Supplier;
import com.hsh.erp.domain.vo.SupplierVO;
import com.hsh.erp.init.DataInitializer;
import com.hsh.erp.mapper.SupplierMapper;
import com.hsh.erp.service.SupplierService;
import com.hsh.erp.utils.MyBeanUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @author USER
 * @description 针对表【supplier】的数据库操作Service实现
 * @createDate 2024-05-30 16:21:43
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier>
        implements SupplierService {

    @Resource
    private DataInitializer dataInitializer;

    /**
     * 获取所有供应商
     *
     * @param supplierVO
     * @return
     */
    @Override
    public Result<List<SupplierDTO>> getAll(SupplierVO supplierVO) {
        List<SupplierDTO> supplierDTOList = DataInitializer.getSupplierDTOList();
        if (supplierDTOList.isEmpty()) {
            return getList(new SupplierVO().setIsDelete(0));
        }
        return Result.success(supplierDTOList);
    }

    /**
     * 列表查询
     *
     * @param supplierVO
     * @return
     */
    @Override
    public Result<List<SupplierDTO>> getList(SupplierVO supplierVO) {
        supplierVO.selectInit();
        QueryWrapper wrapper = new QueryWrapper<>()
                .like(Strings.isNotBlank(supplierVO.getName()), "name", supplierVO.getName())
                .eq(supplierVO.getIsDelete() != null, "is_delete", supplierVO.getIsDelete());
        List<SupplierDTO> supplierDTOList
                = MyBeanUtils.copyList(list(wrapper), SupplierDTO.class);

        return Result.success(supplierDTOList);
    }

    /**
     * 修改一条记录
     *
     * @param supplierVO
     * @return
     */
    @Override
    public Result updateOne(SupplierVO supplierVO) {
        boolean update = updateById(MyBeanUtils.copy(supplierVO, Supplier.class));
        dataInitializer.initSupplier();
        return Result.create(update);
    }


    /**
     * 删除一条记录
     *
     * @param supplierVO
     * @return
     */
    @Override
    public Result deleteOne(SupplierVO supplierVO) {
        return updateOne(supplierVO);
    }


    /**
     * 新增一条记录
     *
     * @param supplierVO
     * @return
     */
    @Override
    public Result addOne(SupplierVO supplierVO) {
        boolean save = save(MyBeanUtils.copy(supplierVO, Supplier.class));
        dataInitializer.initSupplier();
        return Result.create(save);
    }

    /**
     * 根据id获取名称
     *
     * @param id
     * @return
     */
    @Override
    public String getNameById(Integer id) {
        // 获取缓存
        Map<Integer, String> supplierMap = DataInitializer.getSupplierMap();
        // 通过数据库获取并写到缓存，再次获取缓存
        if (supplierMap == null || supplierMap.isEmpty()) {
            supplierMap = dataInitializer.initSupplier();
        }
        return supplierMap.get(id);
    }
    //============================================私有方法开始============================================
    //============================================私有方法结束============================================
}




